package cc.vv.btpartyjob.partyservice.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.adapter.JniViewPageAdapter
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.delegate.TinyWishActivityDelegate
import cc.vv.btpartyjob.partyservice.fragment.MicroDonationFragment
import cc.vv.btpartyjob.partyservice.fragment.TinyWishFragment
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 微心愿
 * Created by Chen.qi on 2018/11/01.
 */
class TinyWishActivity : BaseActivityMVP<TinyWishActivityDelegate>() {

    /**
     * ViewPager适配器
     */
    private var mAdapter: JniViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    /**
     * 当前位置
     */
    private var currentPos: Int = 0

    override fun initData() {
        viewDelegate?.setTopBarTitle("微心愿")
        viewDelegate?.showZuZhiSearch(true)

        mFragments = ArrayList()
        val registerYesFragment = TinyWishFragment()
        val registerNoFragment = MicroDonationFragment()
        mFragments!!.add(registerYesFragment)
        mFragments!!.add(registerNoFragment)
        mAdapter = JniViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setRegisterYesSelect(true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {
            }

            override fun rightOnClick() {

            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //组织查询
            JniStartActivity.startIntentOrganActivity(this, 3, 0, 100)

        }, R.id.iv_atv_zuZhiSearch)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //微心愿
            viewDelegate?.setRegisterYesSelect(true)
            viewDelegate?.setViewPagerPos(0)
        }, R.id.rl_atw_tiny)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //微捐赠
            viewDelegate?.setRegisterYesSelect(false)
            viewDelegate?.setViewPagerPos(1)
        }, R.id.rl_atw_juanZen)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //发布
            val intent = Intent()
            if (0 == currentPos) {
                intent.setClass(this@TinyWishActivity, SubmitTinyWishActivity::class.java)
            } else {
                intent.setClass(this@TinyWishActivity, SubmitMicroDonationActivity::class.java)
            }
            startActivity(intent)
        }, R.id.iv_atv_add)

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //我的认领列表
            val intent = Intent()
            intent.setClass(this@TinyWishActivity, MineClaimActivity::class.java)
            startActivity(intent)
        }, R.id.iv_atv_mine)

        viewDelegate?.bindViewPagerLisener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                currentPos = position
                if (position == 0) {
                    viewDelegate?.setRegisterYesSelect(true)
                } else {
                    viewDelegate?.setRegisterYesSelect(false)
                }
            }
        })
    }

    override fun getDelegateClass(): Class<TinyWishActivityDelegate> {
        return TinyWishActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            val zc = mFragments!![0] as TinyWishFragment
            val yj = mFragments!![1] as MicroDonationFragment
            zc.zuZhiQuery(orgId, type)
            yj.zuZhiQuery(orgId, type)
        }
    }

}